package cn.iocoder.yudao.module.member.controller.app.cardsecurity;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.cardsecurity.vo.*;
import cn.iocoder.yudao.module.member.controller.app.order.vo.AppMemberCertificateOrderVo;
import cn.iocoder.yudao.module.member.convert.cardsecurity.CardSecurityConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.cardsecurity.CardSecurityDo;
import cn.iocoder.yudao.module.member.dal.dataobject.order.MemberCertificateOrderDo;
import cn.iocoder.yudao.module.member.service.cardsecurity.CardSecurityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 卡密管理")
@RestController
@RequestMapping("/member/card/security")
@Validated
public class CardSecurityController {
    @Resource
    private CardSecurityService cardSecurityService;


    @PostMapping("/create")
    @Operation(summary = "生成卡密")
    @PreAuthenticated
    public CommonResult<Boolean> create(@Valid @RequestBody CardSecurityVo cardSecurityVo) {
        cardSecurityService.create(cardSecurityVo);
        return success(true);
    }



    @PostMapping("/exchange")
    @Operation(summary = "兑换卡密")
    public CommonResult<AppMemberCertificateOrderVo> exchange(@Valid @RequestBody CardSecurityExchengeVo cardSecurityExchengeVo) {
        MemberCertificateOrderDo exchange = cardSecurityService.exchange(cardSecurityExchengeVo);
        return success(BeanUtils.toBean(exchange,AppMemberCertificateOrderVo.class));
    }
    /**
     * 卡密分页查询
     */

    @PostMapping("/page")
    @Operation(summary = "获得用户卡密记录分页")
    @PreAuthenticated
    public CommonResult<PageResult<CardSecurityPageReqVo>> page(@Valid @RequestBody CardSecurityQueryVo pageVO) {
        // 执行分页查询
        Long loginUserId = getLoginUserId();
        pageVO.setUserId(loginUserId);
        PageResult<CardSecurityDo> pageResult = cardSecurityService.page(pageVO);

        return success(CardSecurityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/enable")
    @Operation(summary = "启用")
    @PreAuthenticated
    public CommonResult<Boolean> enable(@RequestParam("ids") List<Long> ids) {
        cardSecurityService.enable(ids);
        return success(true);
    }
    @GetMapping("/disable")
    @Operation(summary = "禁用")
    @PreAuthenticated
    public CommonResult<Boolean> disable(@RequestParam("ids") List<Long> ids ) {
        cardSecurityService.disable(ids);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @PreAuthenticated
    public CommonResult<Boolean> delete(@RequestBody DeleteVo deleteVo) {
        cardSecurityService.delete(deleteVo.getIds());
        return success(true);
    }

    @GetMapping("/count")
    @Operation(summary = "查询未使用的卡密")
    @PreAuthenticated
    public CommonResult<Long> count() {
        return success(cardSecurityService.count());
    }

    @GetMapping("/getCardList")
    @Operation(summary = "获取最后一次创建的所有卡密")
    @PreAuthenticated
    public CommonResult<List<String>> getCardList() {
        return success(cardSecurityService.getCardList());
    }



    @PostMapping("/dropSignature")
    @Operation(summary = "掉签校验")
    public CommonResult<Boolean> dropSignature(@Valid @RequestBody CardSecurityExchengeVo cardSecurityExchengeVo) {
        return success(cardSecurityService.dropSignature(cardSecurityExchengeVo));
    }

    @GetMapping("/certCheck")
    @Operation(summary = "证书检测")
    public CommonResult<CertCheckVo> certCheck(@RequestParam("id") Long id) throws Exception {
        return success(cardSecurityService.certCheck(id));
    }

}
